<?php

namespace App\Http\Requests\Api;

use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class DrugStoreRequest extends FormRequest
{
    protected $drug;

    /**
     * Prepare the data for validation.
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->drug = app('field')->get('drug');
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'record_id'   => [
                'required', 'integer', 'min:1'
            ],
            'medication'                              => 'nullable|array',
            'medication.*.name'                       => [
                'required',
                Rule::exists('st_drug_dictionary', 'name')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'medication.*.dosage.type'                => 'in:' . implode(',', $this->drug['dosage'][2]['type']),
            'medication.*.dosage.value'               => 'numeric|min:0|max:999|decimal:2',
            'medication.*.dosage.values'              => 'array|min:1|max:4',
            'medication.*.dosage.values.morning'      => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.atnoon'       => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.evening'      => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.values.before_sleep' => 'numeric|decimal:2|min:0|max:999',
            'medication.*.dosage.unit'                => 'in:' . implode(',', $this->drug['dosage'][2]['unit']),
            'medication.*.start_time'                 => 'date',
            'medication.*.end_time'                   => 'date|after_or_equal:medication.*.start_time',
            'medication.*.usage'                      => 'in:' . implode(',', $this->drug['usage'][2]),
            'medication.*.frequency'                  => 'in:' . implode(',', $this->drug['frequency'][2]),

            'cn_medication.*.drugs.*.name'   => [
                'string',
                Rule::exists('st_drug_dictionary', 'name')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'cn_medication.*.drugs.*.weight' => 'required|numeric|min:0|max:999',
            'cn_medication.*.usage'          => 'required|in:' . implode(',', $this->drug['usage'][1]),
            'cn_medication.*.dosage'         => 'required|in:' . implode(',', $this->drug['dosage'][1]['type']),
            'cn_medication.*.frequency'      => 'required|in:' . implode(',', $this->drug['frequency'][1]),
            'cn_medication.*.dose'           => 'required|integer|min:0|max:999',
        ];
    }
}
